<?php
/**
 * Created by xuan.
 * Author: xuan
 * Date: 2018/8/4
 * Time: 18:24
 */

namespace app\api\model;
use think\Db;
use web\util\WebUtil;

class Goodsorder extends BaseModel
{	
	protected $table = 'goodsorder';
	
	public static function selectOneGoodsorderByID($id){
		$data = model("Goodsorder")->where(array("id"=>$id))->find();
// 		if($data['ispay']==1){
// 			$data['ispay']="待发货";
// 		}elseif($data['ispay']==2){
// 		    $data['ispay']="已取消";
// 		}elseif($data['ispay']==3){
// 			$data['ispay']="已发货";
// 		}elseif($data['ispay']==4){
// 			$data['ispay']="换货";
// 		}elseif($data['ispay']==5){
// 		    $data['ispay']="已完成";
// 		}
		$data['status'] = $data['ispay'];
		$data['ispay'] = WebUtil::getOrderStatusName($data['ispay'],$data['flag']);
		$data['pic'] = get_image($data['pic'],'!200');
		return $data;
	}
	
	public static function selectOneGoodsByOid($all){
		$data = model("Goodsorder")->where(array("oid"=>$all['oid'],"mid"=>$all['mid']))->find();
		return $data;
	}
	
	public static function GoodsorderLists(){
		$data = model("Goodsorder")->select();
		return $data;
	}
	
	public static function addOneGoodsorder($all)
    {
		$data = Goodsorder::create([
			'oid' => $all['oid'],
			'goodsid' => $all['goodsid'],
			'goodsname' => $all['goodsname'],
			'sigprice' => $all['sigprice'],
			'total' => $all['total'],
			'pic' => $all['pic'],
			'mid' => $all['mid'],
			'fid' => $all['fid'],
			'mtel' => $all['mtel'],
			'ispay' => 1,
			'beizhu' => $all['beizhu'],
			'courier_num' => $all['courier_num'],
			'receive_name' => $all['receive_name'],
			'receive_tel' => $all['receive_tel'],
			'receive_addrvalue' => $all['addrvalue'],
			'receive_addr' => $all['receive_addr'],
			'paytime' => date('Y-m-d H:i:s',time()),
			'addtime' => date('Y-m-d H:i:s',time()),
			'updatetime' => date('Y-m-d H:i:s',time()),
		    'receive_addr_id'=>$all['receive_addr_id'],
		    'send_addr_id'=>$all['send_addr_id'],
		    'date'=>date('Ymd'),
		    'reward'=>$all['reward'],
		    'account'=>$all['account'],
		    'reward_account'=>$all['reward_account'],
		    'store_id'=>$all['store_id'],
		    'key'=>$all['key'],
		    'key_name'=>$all['key_name'],
		    'ctime'=>time(),
		]);
		  
		return $data;
    }
	
	public static function updOneGoodsorder($all)
    {
		$data=Db::name('Goodsorder')-> where('id',$all['id'])->update([
			'multipic' => $all['multipic'],
			'Goodsordername' => $all['Goodsordername'],
			'Goodsorderby' => $all['Goodsorderby'],
			'catid' => $all['catid'],
			'isshow' => $all['catid'],
			'stock' => $all['stock'],
			'price' => $all['price'],
			'superreward' => $all['superreward'],
			'bigreward' => $all['bigreward'],
			'agentprice' => $all['agentprice'],
			'detail' => $all['detail'],
			'updatetime' => date('Y-m-d H:i:s',time())
		]);
		return $data;
    }
	
	public static function delOneGoodsorder($id)
    {
		$data=Db::name('Goodsorder')-> where('id',$id)->delete();
		return $data;
    }
	
	/**
     * @param $where
     * @return array|false|\PDOStatement|string|\think\Model
     * 查询详情
     */
    public static function getListByWhere($where, $page = 0, $pageSize = 10){
		$pageSize = $pageSize <= 0 ? 10 : $pageSize;
//         $list = Db::name('Goodsorder')->where($where)->limit($page, $pageSize)->order('id DESC')->select();
        $list = Db::name('Goodsorder')->where($where)->order('id DESC')->select();
		foreach($list as $k=>&$v){
// 			if($v['ispay']==1){
// 				$list[$k]['ispay']="待发货";
// 			}elseif($v['ispay']==3){
// 				$list[$k]['ispay']="已发货";
// 			}elseif($v['ispay']==4){
// 				$list[$k]['ispay']="已完成";
// 			}
		    $v['status'] = $v['ispay'];
		    $v['ispay'] = WebUtil::getOrderStatusName($v['ispay'],$v['flag']);
		    $v['pic'] = get_image($v['pic'],'!200');
		}
		return $list;
    }
	
	public static function getCount($where = ''){
        return model("Goodsorder")->name('goodsorder')->where($where)->count();
    } 
	
	public static function getCount2($where){
        return model("Goodsorder")->name('goodsorder')->where($where)->sum('courier_num');
    } 
	public static function getCount3($where){
        return model("Goodsorder")->name('goodsorder')->where($where)->sum('total');
    } 
	
	public static function setaccount($map){
		$count = Db::name('Member')->where('id',$map['mid'])->update(['account' => $map['account']]);
		return $count;
	}
}